package l1j.opqlo.Server.etcitem;


import l1j.server.Config;
import l1j.server.server.datatables.MagicDollTable;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.Instance.L1DollInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_OwnCharStatus;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SkillIconGFX;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.serverpackets.S_Sound;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1MagicDoll;
import l1j.server.server.templates.L1Npc;

public class MagicDoll {

	/**魔法娃娃*/

	public static void begin(final int[] newData , final L1PcInstance pc , final L1ItemInstance l1iteminstance){
		final int itemObjid = newData[0];
		final int itemId = l1iteminstance.getItemId();

		//TODO 魔法娃娃DB化
		final L1MagicDoll magic_doll = MagicDollTable.getInstance().getTemplate((itemId));

		if( magic_doll == null){ //沒有此娃娃資料
			return;
		}

		final int npcId = magic_doll.getDollId();

		boolean isAppear = true ;
		L1DollInstance doll = null;

		if(!pc.getDollList().isEmpty()){
			for (final L1DollInstance curdoll : pc.getDollList().getAllDolls()) {
				doll = curdoll;
				/*		if(doll.getNpcId() == 710032){
					我有皮卡丘 = true;
				}
				if (doll.getItemObjId() == itemObjid) { // 既に引き出しているマジックドール
					isAppear = false;
					break;
				}*/
				if(doll.getNpcId() == npcId){
					isAppear = false;
					break;
				}else{
					boolean Itemisrepeat = false ,Dollisrepeat=false ;
					if(npcId >=91370 && npcId <=91379){
						for(final int dollid : 聖誕雪人重複){
							if(dollid == npcId){
								Itemisrepeat = true;
							}else if(dollid == doll.getNpcId()){
								Dollisrepeat= true;
							}
						}
					}else if(npcId >=91380 && npcId <=91389){
						for(final int dollid : 飛龍重複){
							if(dollid == npcId){
								Itemisrepeat = true;
							}else if(dollid == doll.getNpcId()){
								Dollisrepeat= true;
							}
						}
					}else if(npcId >=91390 && npcId <=91399){
						for(final int dollid : 女妖精重複){
							if(dollid == npcId){
								Itemisrepeat = true;
							}else if(dollid == doll.getNpcId()){
								Dollisrepeat= true;
							}
						}
					}
					if(Dollisrepeat && Itemisrepeat){
						isAppear = false;
						break;
					}
				}
			}
		}
		if (isAppear) {
			if (!pc.getInventory().checkItem(41246, 50)) {
				pc.sendPackets(new S_ServerMessage(337, "$5240")); // \f1%0が不足しています。
				return;
			}
			if (pc.getDollList().size() >= Config.MAX_DOLL_COUNT) {
				pc.sendPackets(new S_ServerMessage(319));
				return;
			}

			if(itemId == 780043){//opqlo 皮卡丘
				pc.sendPacketsX8(new S_Sound(9997));
			}else if(itemId == 780064){
				pc.sendPacketsX8(new S_Sound(9809));
			}else if(itemId == 78047){//萬聖節:南瓜燈
				pc.sendPacketsX8(new S_Sound(9994));
			}else if(itemId ==77789){
				pc.sendPacketsX8(new S_Sound(6008));
			}else if(itemId == 77790){
				pc.sendPacketsX8(new S_Sound(6010));
			} /*else if (pc.getDollList().size() >= Config.MAX_DOLL_COUNT) {
					if(我有皮卡丘 && (pc.getDollList().size()-1) >= Config.MAX_DOLL_COUNT){
						// \f1これ以上のモンスターを操ることはできません。
						pc.sendPackets(new S_ServerMessage(319));
						return;
					}else if(!我有皮卡丘){
						pc.sendPackets(new S_ServerMessage(319));
						return;
					}
				}*/

			final L1Npc template = NpcTable.getInstance().getTemplate(npcId);
			doll = new L1DollInstance(template, pc, itemId, itemObjid);

			if(itemId == 780043 || itemId == 780064){
				doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 6130));
				pc.sendPackets(new S_SystemMessage("神奇寶貝會保護你。"));
				pc.sendPackets(new S_SkillIconGFX(56, 1800));//TODO 魔法娃娃判斷秒數
			}else{
				if(itemId == 78047){ //萬聖節:南瓜燈
					doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 2761));
				}else if(itemId ==77789 || itemId == 77790){
					doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 6130));
				}else{
					doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 5935));
				}
				pc.sendPackets(new S_SkillIconGFX(56, 1800));//TODO 魔法娃娃判斷秒數
				pc.sendPackets(new S_SystemMessage("魔法娃娃會保護你。"));
			}

			pc.sendPackets(new S_OwnCharStatus(pc));
			pc.getInventory().consumeItem(41246, 50);//TODO 魔法結晶體不足。
		} else {
			if(itemId == 780043){//opqlo 皮卡丘
				doll.broadcastPacketX8(new S_Sound(9996));
				doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 6130));
			}else if( itemId == 780064){//opqlo 小火龍
				doll.broadcastPacketX8(new S_Sound(9809));
				doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 6130));
			}else if(itemId == 78047){//萬聖節:南瓜燈
				doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 2761));
				doll.broadcastPacketX8(new S_Sound(9995));
			}else if(itemId ==77789){
				doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 6130));
				doll.broadcastPacketX8(new S_Sound(6008));
			}else if(itemId == 77790){
				doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 6130));
				doll.broadcastPacketX8(new S_Sound(6011));
			}else{
				doll.broadcastPacketX8(new S_SkillSound(doll.getId(), 5936));
			}
			doll.deleteDoll();
		}
	}
	
	private static final int[] 聖誕雪人重複 = {
		91370	,//	Lv1.聖誕雪人
		91371	,//	Lv2.聖誕雪人
		91372	,//	Lv3.聖誕雪人
		91373	,//	Lv4.聖誕雪人
		91374	,//	Lv5.聖誕雪人
		91375	,//	Lv6.聖誕雪人
		91376	,//	Lv7.聖誕雪人
		91377	,//	Lv8.聖誕雪人
		91378	,//	Lv9.聖誕雪人
		91379	//	超級聖誕雪人
	};
	
	private static final int[] 飛龍重複 = {
		91380	,//	Lv1.飛龍娃娃
		91381	,//	Lv2.飛龍娃娃
		91382	,//	Lv3.飛龍娃娃
		91383	,//	Lv4.飛龍娃娃
		91384	,//	Lv5.飛龍娃娃
		91385	,//	Lv6.飛龍娃娃
		91386	,//	Lv7.飛龍娃娃
		91387	,//	Lv8.飛龍娃娃
		91388	,//	Lv9.飛龍娃娃
		91389	//	特典飛龍娃娃
	};
	private static final int[] 女妖精重複 = {
		91390	,//	Lv1.魔法女妖
		91391	,//	Lv2.魔法女妖
		91392	,//	Lv3.魔法女妖
		91393	,//	Lv4.魔法女妖
		91394	,//	Lv5.魔法女妖
		91395	,//	Lv6.魔法女妖
		91396	,//	Lv7.魔法女妖
		91397	,//	Lv8.魔法女妖
		91398	,//	Lv9.魔法女妖
		91399	//	大地樹女妖

	};
}
